﻿using System.Web.Mvc;
using System.Web.Routing;
using ASPItalia.ModelVirtualCasting.Web.Utils;
using System.Web.DynamicData;
using ASPItalia.ModelVirtualCasting.EntityFramework;
using ASPItalia.ModelVirtualCasting.BackOffice.ObjectModel;
using ASPItalia.ModelVirtualCasting.Web.Utils;
using System.ServiceModel.Activation;
using RouteDebug;
using System.Data.Services;

namespace ASPItalia.ModelVirtualCasting.Web
{
	public class Global : System.Web.HttpApplication
	{
		/* Dynamica Data */
		private static MetaModel s_defaultModel = new MetaModel();
		public static MetaModel DefaultModel
		{
			get
			{
				return s_defaultModel;
			}
            internal set
            {
                s_defaultModel = value;
            }
		}

        private static DynamicDataRoute dynDataRoute;
		public static void RegisterRoutes(RouteCollection routes)
		{
            /* WebForms */
            routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
			routes.IgnoreRoute("{mypath}.jpg/{*pathInfo}");
            routes.IgnoreRoute("{myWebServices}.asmx/{*pathInfo}");
            routes.IgnoreRoute("Contents/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            dynDataRoute = new DynamicDataRoute("admin/{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            };

            routes.Add("dynData", dynDataRoute);

            /* Candidate MVC */
            routes.MapSEORoute(
                "Profile",
                "Profile/{nickname}/",
                new { controller = "Person", Action = "Profile", nickname = "" });

			routes.MapSEORoute(
                "Candidates",
                "Candidate/{action}/{nickname}/",
                new { controller = "Candidate", Action = "Create", nickname = "" });

			/* NEWS */
			routes.MapSEORoute(
				"NewsIndex",
				"News",
				new { controller = "News", Action = "Index" });

			routes.MapSEORoute(
				"NewsDetails",
				"News/{id}/{slug}",
				new { controller = "News", Action = "Details", id="", slug=""});

			/* Route per pagina di login */
            routes.MapRoute(
                "LogOn",
                "login.aspx",
                new { controller = "Account", action = "LogOn", id = "" }
            );

			routes.MapRoute(
				"LogOff",
				"logout.aspx",
				new { controller = "Account", action = "LogOff", id = "" }
			);

			/* Route per MVC */
            routes.MapSEORoute(
                "MVCRoutes",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" },
                new { controller = new ForbiddenValuesConstraint("webapi", "odata", "feed") }
            );
		}

        public static void RegisterServiceRoutes(RouteCollection routes)
        {
            /* Route per servizi */
            routes.Add(new ServiceRoute("webAPI", new WebScriptServiceHostFactory(), typeof(ASPItalia.ModelVirtualCasting.WCF.Services.ModelsManager)));
            routes.Add(new ServiceRoute("feed", new WebServiceHostFactory(), typeof(ASPItalia.ModelVirtualCasting.WCF.Services.SyndicationService)));
            routes.Add(new ServiceRoute("odata", new DataServiceHostFactory(), typeof(ASPItalia.ModelVirtualCasting.WCF.Services.ODataService)));
        }

        public static void RegisterDynamicDataContext()
        {
            /*Dynamic Data*/
            DefaultModel.RegisterContext(typeof(ModelVirtualCastingBackOfficeEntities), new ContextConfiguration() { ScaffoldAllTables = true });
			//DefaultModel.RegisterContext(typeof(ModelVirtualCastingEntities), new ContextConfiguration() { ScaffoldAllTables = true });
        }

		protected void Application_Start()
		{
			RegisterRoutes(RouteTable.Routes);
            RegisterServiceRoutes(RouteTable.Routes);
            RegisterDynamicDataContext();
			SetCustomViewEngine();
            //EnableRouteTesting();
		}

        private void EnableRouteTesting()
        {
            // Debugger dei routes tramite tool di Phil Haack
            // http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

            RouteTable.Routes.Remove(dynDataRoute);
            RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

		#region customViewEngine
		private void SetCustomViewEngine()
		{
			ViewEngines.Engines.Clear();
			ViewEngines.Engines.Add(new SkinnableViewEngine());
		}
		#endregion

		#region Custom factory registration
		private void SetCustomFactory()
		{
			ControllerBuilder.Current.SetControllerFactory(typeof(DynamicControllerFactory));
		}
		#endregion
	}
}
